<template>
  <div class="view-fill">
    <div v-if="!modifyInfo.visible" class="app-container-flex">
      <FullScreen
        v-model="showDialog"
        title="资金项目详情"
        @returnBack="returnBack"
      >
        <template v-slot:full-button>
          <el-button
            v-if="
              infoList.data.status === 2 && $permission('capitalProject:print')
            "
            @click="print"
            >打印</el-button
          >
          <el-button
            v-if="
              $permission('capitalProject:excelDetails')
            "
            @click="exportExcel"
            >导出excel</el-button
          >
          <el-button v-if="canUpdate" @click="update">修改</el-button>
          <el-button v-if="canSubmit" @click="submit">提交</el-button>
          <el-button v-if="canRevoke" @click="revoke">撤回</el-button>
        </template>

        <div class="model-panel">
          <div class="model-panel-title">
            <p>{{ $t('basic_information') }}</p>
          </div>
          <div class="model-panel-body">
            <basic-information :info="infoList"> </basic-information>
          </div>
        </div>

        <div class="model-panel">
          <div class="model-panel-title">
            <Tooltip :text="$t('detailInfo')"></Tooltip>
          </div>
          <div class="model-panel-body">
            <div class="flex_full_item">
              <!-- 表格 -->
              <page-table
                :refresh="tableInfo.refresh"
                :init-curpage="tableInfo.initCurpage"
                v-model:data="tableInfo.data"
                :pager="false"
                :summary="true"
                :check-box="false"
                :tab-index="true"
                :summaryFieldList="tableInfo.summaryFieldList"
                :field-list="tableInfo.fieldList"
                :handle="tableInfo.handle"
                :isDynamic="true"
              >
                <template v-slot:col-assetCapitalProjectValue="scope">
                  <p>{{ scope.row.assetCapitalProjectValue }}</p>
                </template>
                <template v-slot:col-assetName="scope">
                  <p>
                    {{ scope.row.assetName || '-' }}
                  </p>
                </template>
              </page-table>
            </div>
          </div>
        </div>
      </FullScreen>
    </div>
    <device-store-order-modify
      v-if="modifyInfo.visible"
      v-model:visible="modifyInfo.visible"
      :title="modifyInfo.title"
      :id="modifyInfo.id"
      :type="modifyInfo.type"
      :order-id="modifyInfo.orderId"
      @callBack="capitalProjectModifyInfoCallBack"
    />
    <chose-printer :visible="printerInfo.visible" @callBack="printerCallBack" />
  </div>
</template>

<script setup name="capitalProject">
import FullScreen from '@/components/FullScreen/index'
import PageTable from '@/components/PageTable/index'
import chosePrinter from '@/components/ChosePrinter/index'
import {
  excelCapitalProject,
  getAssetStorePrint,
  getCapitalProjectById,
  getCapitalProjectDetailsById,
  submitCapitalProject,
  revokeCapitalProject
} from '@/api/asset'
import BasicInformation from '@/components/BasicInformation/index'
import DeviceStoreOrderModify from './modify'
import { getCustomizeSetting } from '@/api/customize'
import { fatchTemplateByCode } from '@/api/base'
const emit = defineEmits()
const { proxy } = getCurrentInstance()

const props = defineProps(['visible', 'tableRow'])

const infoList = reactive({
  data: {},
  fieldList: [
    {
      label: '项目编号',
      value: 'code',
      type: 'text',
    },
    {
      label: '项目名称',
      value: 'name',
      type: 'text',
    },
    {
      label: '项目类型',
      value: 'assetCapitalSourceDictName',
      type: 'text',
    },
    {
      label: '预算总额',
      value: 'amount',
      type: 'text',
    },
    {
      label: '项目负责人',
      value: 'projectLeaderName',
      type: 'text',
    },
    {
      label: '填报部门',
      value: 'deptName',
      type: 'text',
    },
    {
      label: '填报人',
      value: 'fillUserName',
      type: 'text',
    },
    {
      label: '项目财务负责人',
      value: 'projectFinanceLeaderName',
      type: 'text',
    },
    {
      label: '填报日期',
      value: 'fillTime',
      type: 'text',
    },
    {
      label: '计划开始日期',
      value: 'planStartTime',
      type: 'text',
    },
    {
      label: '计划结束日期',
      value: 'planEndTime',
      type: 'text',
    },

    {
      label: '实际开始日期',
      value: 'realityStartTime',
      type: 'text',
    },
    {
      label: '实际结束日期',
      value: 'realityEndTime',
      type: 'text',
    },
    {
      label: '项目目的',
      value: 'goal',
      type: 'popover',
    },
    {
      label: '项目背景',
      value: 'background',
      type: 'popover',
    },
  ],
})
// 过滤相关配置
const filterInfo = reactive({
  query: {
    id: props.tableRow.id,
    queryParams: {
      orderId: props.tableRow.id,
    }
  },
})
const modifyInfo = reactive({
  visible: false,
  title: '',
  type: 'update',
  id: '',
})
// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  summaryFieldList: [
    { value: 'qty', formatter: proxy.$fn.qtyFormatter },
    { value: 'sumAmount', formatter: proxy.$fn.TripartiteMethodAsset },
    { value: 'primitiveOriginalAmount', formatter: proxy.$fn.TripartiteMethodAsset },
  ],
  fieldList: [],
})

const individualInfo = reactive({
  visible: false,
  query: {},
  info: {},
})
const printerInfo = reactive({
  visible: false,
  data: {},
})
const formInfoData = ref({})

const showDialog = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emit('update:visible', newValue) // 更新父组件visible
  },
})

const canUpdate = computed(() => {
  return (
          proxy.$permission('capitalProject:update')&& infoList.data.status === 1
  )
})
const canSubmit = computed(() => {
  return (
    proxy.$permission('capitalProject:submit') && infoList.data.status === 1
  )
})
const canRevoke = computed(() => {
  return (
    proxy.$permission('capitalProject:revoke') && infoList.data.status === 2
  )
})

watch(
  () => modifyInfo.visible,
  (value) => {
    if (!value) {
      refreshDetail()
    }
  }
)

watch(
  () => props.visible,
  (value) => {
    showDialog.value = value
  },
  {
    immediate: true,
  }
)

let obj = {
  assetCapitalProjectValue: {
    label: '项目使用金额',
    value: 'assetCapitalProjectValue',
    type: 'slot',
    formatter: proxy.$fn.TripartiteMethod,
    show: false,
    hidden: false,
  },
  assetName: {
    label: '资产名称',
    value: 'assetName',
    componentType: 'input',
    show: false,
    type: 'slot',
    hidden: true,
  },
}
tableInfo.fieldList = Object.values({
  ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
  ...obj,
})
getCustomize()

function capitalProjectModifyInfoCallBack() {
  refreshDetail()
}
//打印
function print() {
  fatchTemplateByCode({
    resourceCode: 'capitalProject:print',
    isDefault: 1,
  }).then((res) => {
    if (res && res.success) {
      let temp = proxy.$fn.getPrintTemp(res.data[0])
      getPrintData(temp)
    }
  })
}
function getPrintData(temp) {
  let params = {
    tableKey:
      'capitalProject-com.slip.spd.entity.vo.device.store.AssetStoreOrderDetailVO',
    orderIds: [props.tableRow.id],
  }
  getAssetStorePrint(params).then((res) => {
    if (res.success && res) {
      let data = proxy.$fn.deepClone(res.data)
      let newData = data.map((item) => {
        return {
          ...item,
          ...item.orderInfo,
        }
      })
      proxy.$lodop.preview(temp, newData)
    }
  })
}

//提交
function submit() {
  let ids = []
  ids.push(infoList.data.id)
  submitCapitalProject(ids).then((resp) => {
    if (resp && resp.success) {
      refreshDetail()
    }
  })
}
function revoke() {
  let ids = []
  ids.push(infoList.data.id)
  revokeCapitalProject(ids).then((resp) => {
    if (resp && resp.success) {
      refreshDetail()
    }
  })
}

//导出excel
function exportExcel() {
  excelCapitalProject(filterInfo.query).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
function refreshDetail() {
  getMain()
}
function returnBack() {
  showDialog.value = false
  emit('returnBack', false)
}

function getMain() {
  getCapitalProjectById(props.tableRow.id).then((resp) => {
    if (resp && resp.success) {
      infoList.data = resp.data

      getList()
    }
  })
}

function update() {
  modifyInfo.id = infoList.data.id
  modifyInfo.title = '修改资金项目'
  modifyInfo.visible = true
}
function getList() {
  getCapitalProjectDetailsById(props.tableRow.id).then((resp) => {
    if (resp && resp.success) {
      tableInfo.data = resp.data
    }
  })
}

function printerCallBack(printer) {
  if (printer !== false) {
    proxy.$print.buildRenderBarCode(printerInfo.data, printer)
  }
  printerInfo.visible = false
}
function getCustomize() {
  let param = {
    tableKey: 'coreAssetCardDetailList-com.slip.spd.entity.vo.core.CoreAssetVO',
  }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      filterInfo.query.customizeUserVO = data
      let obj = proxy.$fn.setFieldList(tableInfo.fieldList, data.detail, true)
      tableInfo.fieldList = obj.fieldList
      getMain()
    }
  })
}
</script>

<style scoped></style>
